package study.springcloud.gateway.filter.gateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Slf4j
@Component
public class CustomGatewayFilterFactory extends AbstractGatewayFilterFactory {

    @Override
    public GatewayFilter apply(Object config) {
        return new MyGatewayFilter();
    }

    /**
     * 网关过滤器
     */
    public class MyGatewayFilter implements GatewayFilter {

        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            log.info(">>>>>>>> custom GatewayFilter, order= {}", 2);
            //next filter
            //chain.filter(exchange)之前的就是 “pre” 部分，之后的也就是then里边的是 “post” 部分
            return chain.filter(exchange);
        }
    }
}
